﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Drawing;

namespace CoreEngine.Util
{
    public class DevExpressDataGridUtil
    {
        #region Field 
        public ArrayList parameter;
        public ArrayList parameterFormatList = new ArrayList();
        public ArrayList parameterList = new ArrayList();

        #endregion
        public enum FormatType
        {
            Number,
            String,
            DateTime
        }
        public ArrayList ParameterFormatList
        {
            get { return parameterFormatList; }
        }
        public ArrayList ParameterList
        {
            get { return parameterList; }
        }
        public static void Fill(DevExpress.XtraGrid.GridControl gridControl, DataSet dataSource)
        {
            ((DevExpress.XtraGrid.Views.Grid.GridView)(gridControl.DefaultView)).Columns.Clear();
            if (dataSource != null && dataSource.Tables.Count > 0)
            {
                gridControl.DataSource = dataSource.Tables[0];
            }
            else
            {

                DataTable dtNew = (DataTable) gridControl.DataSource;
                if (dtNew != null)
                {
                    dtNew.Rows.Clear();
                    gridControl.DataSource = dtNew;
                }
            }
        }
        public static void Fill(DevExpress.XtraGrid.GridControl gridControl,DataTable dataSource)
        {
            ((DevExpress.XtraGrid.Views.Grid.GridView)(gridControl.DefaultView)).Columns.Clear();
            if (dataSource != null)
            {
                gridControl.DataSource = dataSource;
            }
        }
        public static string GetDataValue(DevExpress.XtraGrid.Views.Grid.GridView gridView, string columnValueName)
        {
            try
            {
                DataRowView rowObject = (DataRowView)gridView.GetFocusedRow();

                return "" + rowObject.Row[columnValueName];
            }
            catch  { return ""; }
        }
        public static DataSet GetDataSetSelect(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            {
                int[] rowSelect = gridView.GetSelectedRows();
                DataTable dt = new DataTable();
                DataView dtSource = (DataView)gridView.DataSource;
                for (int j = 0; j < dtSource.Table.Columns.Count; j++)
                {
                    dt.Columns.Add(dtSource.Table.Columns[j].ColumnName);
                }
                for (int l = 0; l < rowSelect.Length; l++)
                { 
                    for (int k = 0; k < dtSource.Table.Rows.Count; k++)
                    {
                        if (rowSelect[l] == k)
                        {
                            DataRow dr = dtSource.Table.Rows[k];
                            DataRow drIn = dt.NewRow();
                            for (int h = 0; h < dr.ItemArray.Length; h++)
                            {
                                drIn[h] = dr[h];
                            } 
                            dt.Rows.Add(drIn);
                            break;
                        }
                    }
                }
                DataSet ds=new DataSet("ITEM");
                ds.Tables.Add(dt.Copy());
                return ds;
            }
            catch { return null; }
        }
        public static DataSet GetDataSet(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            { 
                DataTable dt = new DataTable();
                DataView dtSource = (DataView)gridView.DataSource;
                for (int j = 0; j < dtSource.Table.Columns.Count; j++)
                {
                    dt.Columns.Add(dtSource.Table.Columns[j].ColumnName);
                }
                for (int k = 0; k < dtSource.Table.Rows.Count; k++)
                {
                    DataRow dr = dtSource.Table.Rows[k];
                    DataRow drIn = dt.NewRow();
                    for (int h = 0; h < dr.ItemArray.Length; h++)
                    {
                        drIn[h] = dr[h];
                    }
                    dt.Rows.Add(drIn); 
                }
                DataSet ds = new DataSet("ITEM");
                ds.Tables.Add(dt.Copy());
                return ds;
            }
            catch { return null; }
        }
        public static DataSet GetDataSetChecked(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            {
                ////int[] rowSelect = gridView.GetSelectedRows();
                DataTable dt = new DataTable();
                DataView dtSource = (DataView)gridView.DataSource;
                for (int j = 0; j < dtSource.Table.Columns.Count; j++)
                {
                    dt.Columns.Add(dtSource.Table.Columns[j].ColumnName);
                }
                DataRow[] rowSelected = dtSource.Table.Select("Selected=True");
               // dt.Rows.Add(rowSelected);
                foreach (DataRow dr in rowSelected)
                {
                    DataRow drs = dt.NewRow();
                   // drs = dr;
                    for (int k = 0; k < dr.ItemArray.Length; k++)
                    {
                        drs[k] = dr.ItemArray[k];
                    } 
                    dt.Rows.Add(drs);
                }
                //for (int k = 0; k < dtSource.Table.Rows.Count; k++)
                //{
                //    if ((bool)gridView.GetRowCellValue(k, "Selected"))
                //    {
                //        DataRow dr = dtSource.Table.Rows[k];
                //        DataRow drIn = dt.NewRow();
                //        for (int h = 0; h < dr.ItemArray.Length; h++)
                //        {
                //            drIn[h] = dr[h];
                //        }
                //        dt.Rows.Add(drIn); 
                //    }
                //}
                DataSet ds = new DataSet("ITEM");
                ds.Tables.Add(dt.Copy());
                return ds;
            }
            catch { return null; }
        }
        public static void SetCurrentRow(DevExpress.XtraGrid.Views.Grid.GridView gridView, string columnValueName, string valueName)
        {
            try
            {
                DataView dt = (DataView)gridView.DataSource;
                for (int k = 0; k < dt.Table.Rows.Count; k++)
                {
                    if (("" + dt.Table.Rows[k][columnValueName]).Trim() == valueName.Trim())
                    {
                        gridView.UnselectRow(gridView.FocusedRowHandle);
                        gridView.RefreshData();
                        gridView.ResetCursor();
                        gridView.RefreshRow(k);  
                        gridView.FocusedRowHandle = k;
                        gridView.SelectRow(k); 
                        break;
                    }
                }
            }
            catch { }
        }
        public void SetParameterColumn(string FieldName, string valueDisplay, bool columnDisplay, int width, DevExpress.Utils.HorzAlignment alignment)
        {
            parameter = new ArrayList(); 
            parameter.Add(FieldName);
            parameter.Add(valueDisplay);
            parameter.Add(width);
            parameter.Add(columnDisplay);
            parameter.Add(alignment);
            ParameterList.Add(parameter);
        }
        public void SetParameterColumnFormat(string FieldName, FormatType formatType, string Format)
        { 
                parameter = new ArrayList();
                parameter.Add(FieldName);
                parameter.Add(formatType);
                parameter.Add(Format);
                ParameterFormatList.Add(parameter); 
        }
        public void GridViewRanderConfig(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            { 
                for (int l = 0; l < ParameterList.Count; l++)
                {
                    try
                    {
                        ArrayList parameter = (ArrayList)ParameterList[l];
                        string colOld = (string)parameter[0];
                        string colNew = (string)parameter[1];
                        int width = (int)parameter[2];
                        bool visible = (bool)parameter[3];
                        DevExpress.Utils.HorzAlignment ali = (DevExpress.Utils.HorzAlignment)parameter[4];
                        gridView.Columns[colOld].VisibleIndex = l;
                        if (width != 0)
                        {
                            gridView.Columns[colOld].Width = width;
                        }
                        gridView.Columns[colOld].Caption = colNew;
                        gridView.Columns[colOld].Visible = visible;
                        //DB SaiKrok X, 18pt
                        gridView.Columns[colOld].AppearanceHeader.Font = new Font("DB SaiKrok X", 18F, FontStyle.Bold);
                        gridView.Columns[colOld].AppearanceHeader.TextOptions.HAlignment = ali;
                        gridView.Columns[colOld].AppearanceCell.TextOptions.HAlignment = ali;
                    }
                    catch { }
                }
            }
            catch { }
        }
        public void GridViewRanderDataFormat(DevExpress.XtraGrid.GridControl gridControls)
        {
            try
            {
                DataTable dt = (DataTable)gridControls.DataSource;
                DataTable dtNew = new DataTable();
                //CreateColumn
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    dtNew.Columns.Add(dt.Columns[c].ColumnName);
                }
                //dtNew.Rows.Clear();
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    DataRow drs=dtNew.NewRow();
                    for (int co = 0; co < dt.Columns.Count; co++)
                    {
                        drs[dt.Columns[co].ColumnName] = GetValueRevertFormat(""+dt.Rows[j][co], dt.Columns[co].ColumnName);
                    }
                    dtNew.Rows.Add(drs);
                }
                gridControls.DataSource = dtNew; 
            }
            catch { }
            
        }
        private string GetValueRevertFormat(string value, string columnName)
        {
            try
            {
                //string valueReturn = "";
                foreach (ArrayList parameterAll in ParameterFormatList)
                {
                    string fieldName = "" + parameterAll[0];
                    string formatType = "" + parameterAll[1];
                    string format = "" + parameterAll[2];
                    if (columnName.Trim() == fieldName.Trim())
                    {
                        switch (formatType)
                        {
                            case "Number": value = String.Format(format, Double.Parse("0" + value));break;
                            case "String": value = String.Format(format, "" + value); break;
                            //case "DateTime":
                            //    value = (("" + value) != "") ? "" : String.Format(format, DateTimeUtil.ConvertStringDateTimeFormatyyyyMMdd("" + value)); break;
                        }
                        return value;
                    }
                }
            }
            catch { }
            return value;
        }
        public static void GridViewCaptionB(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            for (int k = 0; k < gridView.Columns.Count; k++)
            {
                gridView.Columns[k].AppearanceHeader.Font = new Font("Tahoma", 8F, FontStyle.Bold);
            }
        }
        public static void BestFitAllColumn(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            {
                for (int k = 0; k < gridView.Columns.Count; k++)
                {
                    if (gridView.Columns[k].Visible)
                    {
                        gridView.Columns[k].BestFit();
                    }
                }
            }
            catch { }
        }
        public static void BestFitByColumn(DevExpress.XtraGrid.Views.Grid.GridView gridView, int columnNumber)
        {
            try
            {
                gridView.Columns[columnNumber].BestFit();
            }
            catch { }
        }
        public static void BestFitByColumn(DevExpress.XtraGrid.Views.Grid.GridView gridView, string columnName)
        {
            try
            {
                gridView.Columns[columnName].BestFit();
            }
            catch { }
        }
    }
}
